var index0 = 0;
var index1 = 0;
var index2 = 0;

$(document).ready(function () {
    $(".yq-tab-head-item").click(function () {
        $(".yq-tab-head-item").eq($(this).index()).addClass("active").siblings().removeClass('active');
        $(".yq-tab-content-item").hide().eq($(this).index()).show();

        if ($(this).index() == 0) {
            if (index0 == 1) {
                return false;
            }
            index0 = 1;
            getDept($('#hy'), "hy");
        } else if ($(this).index() == 1) {
            if (index1 == 1) {
                return false;
            }
            index1 = 1;
            getDept($('#zd'), "zd");
        } else {
            if (index2 == 1) {
                return false;
            }
            index2 = 1;
            getDept($('#hj'), "hj");
        }
    });

    setTimeout(function() {
        getDept($('#hy'), "hy");
    }, 500);
    index0 = 1;
});

function getDept(obj, hospitalId) {

    if (hospitalId == "") {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer;
            layer.msg('请选择院区！', {
                icon: 0
            });
        });
        return false;
    }

    // 遮罩
    var load;
    layui.use(['layer', 'form'], function () {
        var layer = layui.layer;
        load = layer.load();
    });
    $.ajax({
        url: "reserve/deptListAjax",
        type: "post", // 数据发送方式
        dataType: "json", // 接受数据格式
        data: {"hospitalId": hospitalId},
        success: function (jsonData) {
            // 隐藏遮罩层
            layui.use(['layer', 'form'], function () {
                var layer = layui.layer;
                layer.close(load);
            });
            if (jsonData.rc == 1) {
                var html = [];
                if (jsonData.getDeptmentListOutInfoArr) {
                    $.each(jsonData.getDeptmentListOutInfoArr.getDeptmentListOutInfo, function (i, item) {
                        if (item.deptlevel == "1") {
                            html.push("<dl>");
                            html.push("<dt><span>" + item.deptname + "</span></dt>");
                            html.push("<dd>");
                            var hospitalcode = item.hospitalid;
                            if(item.deptcode == '1S222'){
                                hospitalcode = '432';
                            }
                            $.each(jsonData.getDeptmentListOutInfoArr.getDeptmentListOutInfo, function (h, dept) {
                                if (item.deptcode == dept.parentid) {
                                    var deptstr = "";
                                    $.each(jsonData.getDeptmentListOutInfoArr.getDeptmentListOutInfo, function (j, dept2) {
                                        if (dept.deptcode == dept2.parentid) {
                                            deptstr = deptstr + dept2.deptcode + ",";
                                        }
                                    });
                                    if(deptstr == ''){
                                        deptstr = '0';
                                    }
                                    html.push("<button type=\"button\" class=\"yq-btn\" onclick=\"getDoctorList('" + hospitalcode + "','" + dept.deptcode + "','" + deptstr + "','" + dept.deptname + "')\"><span>" + dept.deptname + "</span></button>");
                                }
                            });
                            html.push("</dd>");
                            html.push("</dl>");
                        }
                    });
                }
                $(obj).html(html);
            } else {
                layui.use(['layer', 'form'], function () {
                    var layer = layui.layer;
                    layer.msg(jsonData.errorMsg, {
                        icon: 0
                    });
                });
            }
        },
        error: function (e) {
            // 隐藏遮罩层
            layui.use(['layer', 'form'], function () {
                var layer = layui.layer;
                layer.close(load);
            });
        }
    });
}

function getDoctorList(hospitalid, deptcode, deptstr, deptname) {
    window.location.href = projectName+'/reserve/' + hospitalid + '/doctorSchedule/' + deptcode + '/' + deptstr + '/' + deptname + '/0';
}